﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HHPS.CreditEducation.DataProvider;
using HHPS.CreditEducation.Frameworks.Commons;
using HHPS.CreditEducation.Frameworks.Constants;

namespace HHPS.CreditEducation.Presentation.StudyPlans
{
    public partial class ManagementControl : UserControl
    {
        private readonly Business.Business _business;

        public ManagementControl()
        {
            InitializeComponent();

            _business = MainView.Instance.CreditEducationBusiness;
        }

        #region Load Data

        private void Management_Load(object sender, EventArgs e)
        {
            Dock = DockStyle.Fill;
            LoadMajor();
            LoadStudyPlanManagement();
        }

        private void LoadMajor()
        {
            cboMajor.DataSource = _business.GetListMajor();
            cboMajor.DisplayMember = (string)typeof(Major).Properties().Name;
            cboMajor.ValueMember = (string)typeof(Major).Properties().Id;
        }

        private void LoadStudyPlanManagement()
        {
            if (cboMajor.Items.Count > 0)
            {
                dgvStudyPlans.DataSource = ((Major)cboMajor.SelectedItem).StudyPlans.ToList();

                DataGridViewHelper.ShowHideColumns(dgvStudyPlans,
                                                   new[]
                                                       {
                                                           PropertiesConstants.IsChecked, PropertiesConstants.Name,
                                                           PropertiesConstants.StartDate
                                                       });
                DataGridViewHelper.ShowIsCheck(dgvStudyPlans);
                DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvStudyPlans, false);
            }
        }

        private void ComboBoxMajor_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadStudyPlanManagement();
        }

        private void DgvStudyPlans_CurrentCellChanged(object sender, EventArgs e)
        {
            StudyPlanSelectChanged();
        }

        private void StudyPlanSelectChanged()
        {
            if (dgvStudyPlans.RowCount > 0 && dgvStudyPlans.CurrentRow != null)
            {
                txtStudyPlan.Text = dgvStudyPlans.CurrentRow.Cells[PropertiesConstants.Name].Value.ToString();
                txtDetail.Text = dgvStudyPlans.CurrentRow.Cells[new StudyPlan().Properties.Detail].Value.ToString();
                dtpStartDate.Value = (DateTime)dgvStudyPlans.CurrentRow.Cells[PropertiesConstants.StartDate].Value;
            }
            else
            {
                txtStudyPlan.Text = string.Empty;
                txtDetail.Text = string.Empty;
                dtpStartDate.Value = DateTime.Now;
                ckbAllFaculty.Checked = false;
            }
        }

        private void DgvStudyPlans_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvStudyPlans == null ||
                dgvStudyPlans.Columns == null ||
                dgvStudyPlans.Columns[(string)typeof(Faculty).Properties().IsChecked] == null ||
                dgvStudyPlans.Columns[(string)typeof(Faculty).Properties().IsChecked].Index != e.ColumnIndex)
                return;

            dgvStudyPlans[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvStudyPlans[e.ColumnIndex, e.RowIndex].Value;

            ckbAllFaculty.Checked = DataGridViewHelper.AllRowsIsCheck(dgvStudyPlans);
        }

        private void CkbAllFaculty_CheckedChanged(object sender, EventArgs e)
        {
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvStudyPlans, ckbAllFaculty.Checked);
        }

        #endregion

        #region Buttons Click

        private void ButtonGo_Click(object sender, EventArgs e)
        {
            //go to other screen
            if (dgvStudyPlans.RowCount > 0 && dgvStudyPlans.CurrentRow != null)
            {
                string nameScreen = dgvStudyPlans.CurrentRow.
                    Cells[(string)typeof(StudyPlan).Properties().Name].Value.ToString();
                
                MainView.Instance.GoToStudyPlanScreen(nameScreen);
            }
        }

        private void ButtonAddFaculty_Click(object sender, EventArgs e)
        {
            try
            {
                var plan = new StudyPlan
                               {
                                   IdMajor = ((Major)cboMajor.SelectedItem).Id,
                                   Name = txtStudyPlan.Text.Trim(),
                                   Detail = txtDetail.Text.Trim(),
                                   StartDate = dtpStartDate.Value
                               };
                string message;

                if (_business.AddNewStudyPlanValidate(plan, out message))
                {
                    _business.AddNewStudyPlan(plan);

                    LoadStudyPlanManagement();
                    MainView.Instance.RefreshWhenAddStudyPlan();
                }
                else
                    MessageBox.Show(message);
            }
            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }

        private void ButtonEditFaculty_Click(object sender, EventArgs e)
        {
            if (dgvStudyPlans.CurrentRow != null)
            {
                var studyPlan = new StudyPlan
                {
                    Id = Convert.ToInt32(dgvStudyPlans.CurrentRow.Cells[(string)typeof(Faculty).Properties().Id].Value),
                    Name = txtStudyPlan.Text.Trim(),
                    Detail = txtDetail.Text.Trim(),
                    StartDate = dtpStartDate.Value
                };
                string message;

                if (_business.EditStudyPlanValidator(studyPlan, out message))
                {
                    if (!_business.EditStudyPlan(studyPlan))
                    {
                        MessageBox.Show(ConstantMessages.SaveItemNotSuccess, FileConstants.PROGRAM_NAME);
                        return;
                    }
                    LoadStudyPlanManagement();
                    MainView.Instance.RefreshWhenAddStudyPlan();
                }
                else
                    MessageBox.Show(message);
            }
            else
            {
                MessageBox.Show(ConstantMessages.NoSelected, FileConstants.PROGRAM_NAME);
            }
        }

        private void ButtonDeleteFaculty_Click(object sender, EventArgs e)
        {
            try
            {
                var studyPlans = new List<StudyPlan>();
                string message;

                for (var i = 0; i < dgvStudyPlans.RowCount; i++)
                {
                    if (!(bool)dgvStudyPlans[(string)typeof(StudyPlan).Properties().IsChecked, i].Value) continue;

                    studyPlans.Add(new StudyPlan
                    {
                        Id = (int)dgvStudyPlans[(string)typeof(StudyPlan).Properties().Id, i].Value,
                        Name = dgvStudyPlans[(string)typeof(StudyPlan).Properties().Name, i].Value.ToString()
                    });
                }

                if (studyPlans.Count < 1)
                {
                    MessageBox.Show(ConstantMessages.NoSelected, FileConstants.PROGRAM_NAME);
                    return;
                }

                if (_business.DeleteStudyPlansValidator(studyPlans, out message))
                {
                    if (MessageBox.Show(string.Format(ConstantMessages.ConfirmDeleteItem, " các kế hoạch đã chọn"), FileConstants.PROGRAM_NAME, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        _business.DeleteStudyPlans(studyPlans);

                        LoadStudyPlanManagement();
                        MainView.Instance.RefreshWhenAddStudyPlan();
                    }
                }
                else MessageBox.Show(message);
            }
            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }

        #endregion
    }
}